<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 搜索与添加区域 -->
        <el-row :gutter="0">
          <el-col :span="2">
            <el-button type="primary" @click="goAddPage">添加</el-button>
          </el-col>
          <el-col :span="4">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              :clearable="true"
              @clear="getProductsList"
            >
              <el-button slot="append" icon="el-icon-search" @click="getProductsList"></el-button>
            </el-input>
          </el-col>
        </el-row>
      </div>

      <!-- 用户列表区域 -->
      <el-table :data="productList" :stripe="true" style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="name" label="商品名"></el-table-column>
        <el-table-column prop="price" label="价格(￥)" width="100"></el-table-column>
        <el-table-column prop="weight" label="重量(g)" width="70"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="170">
          <template slot-scope="{row}">{{row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="170">
          <template slot-scope="{row}">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="dialogVisibleEditShow(row.id)"
            ></el-button>

            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteById(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="pageSizes"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改对话框 -->
    <el-dialog title="修改" :visible.sync="dialogVisibleEdit" width="50%">
      修改的form
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="editFormOp">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      dialogVisibleEdit: false,
      // 获取商品列表的参数
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10,
      },
      // 分頁組件
      total: 0,
      pageSizes: [10, 15, 20],
      productList: [],
    }
  },
  created() {
    this.getProductsList()
  },
  methods: {
    // 修改表单显示
    dialogVisibleEditShow(id) {
      console.log(id)
      this.dialogVisibleEdit = true
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.queryInfo.pageSize = val
      this.getProductsList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.queryInfo.pageNum = val
      this.getProductsList()
    },
    // 修改表单操作
    editFormOp() {
      this.dialogVisibleEdit = false
    },
    goAddPage() {
      this.$router.push('/products/add')
    },
    async deleteById(id) {
      const confirm = await this.$confirm(
        window.defineGlobalConstData.delConfirm,
        window.defineGlobalConstData.delTitle,
        {
          confirmButtonText: window.defineGlobalConstData.delTrue,
          cancelButtonText: window.defineGlobalConstData.delFalse,
          type: 'warning',
        }
      ).catch((err) => err)

      if (confirm !== 'confirm' || confirm === 'cancel') {
        return this.$message.info(window.defineGlobalConstData.delCancel)
      }

      await this.$http
        .delete(`products/${id}`)
        .then((res) => {
          res = res.data
          if (!res.code || res.code == 0)
            return this.$message.error(
              res.msg || window.defineGlobalConstData.opError
            )

          this.$message.success(
            res.msg || window.defineGlobalConstData.opSuccess
          )
          this.getProductsList()
        })
        .catch((err) => {
          return this.$message.error(err.message)
        })
    },
    async getProductsList() {
      await this.$http
        .get(`products`, { params: this.queryInfo })
        .then((res) => {
          res = res.data
          if (!res.code || res.code == 0)
            return this.$message.error(
              res.msg || window.defineGlobalConstData.getError
            )

          this.productList = res.data.list
          this.total = res.data.total
          /* this.$message.success(
            res.msg || window.defineGlobalConstData.getSuccess
          ) */
        })
        .catch((err) => {
          return this.$message.error(err.message)
        })
    },
  },
  computed: {},
}
</script>

<style lang="scss" scoped>
</style>